package com.zllkj.mm.adapter

import android.view.MotionEvent
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zllkj.mm.R
import com.zllkj.mm.model.entity.GridImageViewBean
import com.zllkj.mm.module.moment.FriendDetailFragment
import com.zllkj.mm.module.moment.FriendListFragment
import com.zllkj.mm.module.shopping.vm.ItemFriendListVM
import com.zllkj.mm.util.common.FormatUtil
import com.zllkj.mm.util.common.PictureLoadUtil
import com.zllkj.mm.util.common.PictureLoadUtil.Companion.loadPic


class NearByFriendListAdapter(layoutResId: Int, data: List<ItemFriendListVM?>?,val fragment: FriendListFragment) :
        BaseQuickAdapter<ItemFriendListVM, BaseViewHolder>(layoutResId, data) {


    override fun convert(baseViewHolder: BaseViewHolder, vm: ItemFriendListVM) {
        var gridView = baseViewHolder.getView<RecyclerView>(R.id.rv_nine_data)
        gridView.setOnTouchListener { v, event ->
            if (event.action == MotionEvent.ACTION_UP)
                fragment.addFragment(FriendDetailFragment(vm))
            false
        }

        baseViewHolder.addOnClickListener(R.id.rv_nine_data)
        baseViewHolder.addOnClickListener(R.id.moreIv)
        baseViewHolder.addOnClickListener(R.id.ll_like_area)

        if (vm.userDyBean?.liked == 0) {
            baseViewHolder.getView<AppCompatImageView>(R.id.iv_like_image).setImageResource(R.mipmap.ic_great)
        } else {
            baseViewHolder.getView<AppCompatImageView>(R.id.iv_like_image).setImageResource(R.mipmap.ic_like_true)
        }

        if(vm.imageList.size == 1 && vm.imageList[0].url == "") {
            gridView.visibility = View.GONE
        }
        gridView.layoutManager = GridLayoutManager(mContext,3)
        var adapter = object : BaseQuickAdapter<GridImageViewBean,BaseViewHolder>(R.layout.item_9_friend_image,vm.imageList){
            override fun convert(p0: BaseViewHolder?, p1: GridImageViewBean?) {
                p1?.let {
                    if (it.url != null) {
                        p0?.getView<AppCompatImageView>(R.id.iv_nine_image)?.let { it1 ->
                            loadPic(it1, p1.url!!)
                        }
                    }
                    if (it.videoUrl != null) {
                        p0?.getView<AppCompatImageView>(R.id.iv_video_play_icon)?.visibility = View.VISIBLE
                        if(it.url == null)
                            p0?.getView<AppCompatImageView>(R.id.iv_nine_image)?.setImageResource(R.drawable.image_loading)
                    }
                }
            }
        }
        gridView.adapter = adapter
        vm.userDyBean?.let {
            var ivHead = baseViewHolder.getView<AppCompatImageView>(R.id.ivhead)
            baseViewHolder.getView<AppCompatTextView>(R.id.tvUserName).text = it.nikeName
            baseViewHolder.getView<AppCompatTextView>(R.id.tv_item_friend_content).text = it.content
            baseViewHolder.getView<AppCompatTextView>(R.id.tv_item_friend_comment_num).text = it.commentNum.toString()
            baseViewHolder.getView<AppCompatTextView>(R.id.tv_item_friend_like_num).text = it.likeNum.toString()
            var address = "|" + it.province + it.city + it.district
            baseViewHolder.getView<AppCompatTextView>(R.id.tv_age_and_sex).text = if(it.sex == 1)"男" else "女"
            baseViewHolder.getView<AppCompatTextView>(R.id.tv_age_and_sex).append(address)
            PictureLoadUtil.loadPic(ivHead,vm.userDyBean!!.userLogo)

            if (it.dynamicType == 1) {
                it.price?.let { price ->
                    baseViewHolder.getView<LinearLayoutCompat>(R.id.ll_invite_time_area).visibility = View.VISIBLE
                    baseViewHolder.getView<AppCompatTextView>(R.id.tv_friend_invite_money).text = FormatUtil.bigDecimalUtilFormat(price, "100") + "/次"
                    baseViewHolder.getView<AppCompatTextView>(R.id.tv_friend_invite_time).text = FormatUtil.dataFormat(it.createTime)
                }
            }
            baseViewHolder.getView<AppCompatTextView>(R.id.tv_item_friend_like_num).text = it.likeNum.toString()

        }


    }


}